home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Programming / MUI / MCC_HTMLtext / Developer / C / Examples / HTMLtext-DEMO.c
C/C++ Source or Header  |  1997-09-11  |  9KB  |  301 lines

  1. // **********************************
  2. // HTMLtext-DEMO
  3. // (C)opyright by Dirk Holtwick, 1997
  4. // **********************************
  5.  
  6. // This demo is kind of a WYSIWYG editor for HTML.
  7. // Just edit the string in the gadget and you will
  8. // see the effect in the same moment.
  9.  
  10. // If you have problems, please report me the bug
  11. // and send me the text that leaded to confusion.
  12.  
  13. /// Includes
  14. #include <workbench/startup.h>
  15. #include <clib/alib_protos.h>
  16. #include <proto/dos.h>
  17. #include <proto/exec.h>
  18. #include <proto/intuition.h>
  19. #include <proto/graphics.h>
  20. #include <proto/asl.h>
  21. #include <proto/utility.h>
  22. #include <proto/muimaster.h>
  23. #include <proto/icon.h>
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include <mui/HTMLtext_mcc.h>
  27.  
  28. #define MAKE_ID(a,b,c,d) ((ULONG) (a)<<24 | (ULONG) (b)<<16 | (ULONG) (c)<<8 | (ULONG) (d))
  29.  
  30. extern struct Library *SysBase;
  31.  
  32. /*  ATTENTION !!!
  33. **  HTMLtext.mcc makes use of recursion and so you
  34. **  have to take care, that there is always
  35. **  enough space on the stack. The more the better!
  36. */
  37.  
  38. LONG __stack = 8192;
  39. ///
  40.  
  41. /// Demotext
  42. #define DEMOTEXT \
  43.    "<html>This <i>is a <small>small</small> <big>Demo</big></i> of " \
  44.    "the <b>HTMLtext class.</b><br> "\
  45.    "Try to drop a HTML file in this window! " \
  46.    "<p align=right>Yours Dirk</html>"
  47. ///
  48. /// Pointers
  49. APTR app;
  50. APTR window;
  51. APTR str;
  52. APTR html;
  53. APTR scroll;
  54. APTR url;
  55. ///
  56.  
  57. /// OpenURLHook
  58. __saveds ULONG __asm OpenURL(register __a0 struct Hook *h, register __a2 Object *obj, register __a1 struct{ char *url; char *tmpname; } *msg)
  59. {
  60.    /*
  61.     *    I know that this function and the following are quite
  62.     *    ugly and I will try to write a better example, soon.
  63.     */
  64.  
  65.    FILE *f;
  66.  
  67.    // Is it HTML ...
  68.    if(!stricmp(msg->url+strlen(msg->url)-5,".html"))
  69.    {
  70.       // puts("Open HTML");
  71.       if(f=fopen(msg->tmpname,"w"))
  72.       {
  73.          fprintf(f,"Simulated copying of URL '%s' to '%s'.",msg->url,msg->tmpname);
  74.          fclose(f);
  75.          return(TRUE);
  76.       }
  77.    }
  78.  
  79.    // or an image ...
  80.    else
  81.    {
  82.       // puts("Open IMAGE");
  83.       strcpy(msg->tmpname, "default.img");
  84.       return(TRUE);
  85.    }
  86.  
  87.    return(FALSE);
  88. }
  89. ///
  90. /// CloseURLHook
  91. __saveds ULONG __asm CloseURL(register __a0 struct Hook *h, register __a2 Object *obj, register __a1 struct{ char *tmpname; } *msg)
  92. {
  93.    // Delete temporary file
  94.    if(!strnicmp(msg->tmpname, "t:", 2))
  95.    {
  96.       // puts("Close URL");
  97.       remove(msg->tmpname);
  98.    }
  99.    else
  100.    {
  101.       ;
  102.       // puts("Close IMAGE");
  103.    }
  104.  
  105.    return(0);
  106. }
  107. ///
  108. /// VLinkHook
  109. static BOOL followed;
  110.  
  111. __saveds ULONG __asm VLink(register __a0 struct Hook *h, register __a2 Object *obj, register __a1 struct{ char *url; } *msg)
  112. {
  113.    /*
  114.     *    This is a really simple example. It just mixes followed
  115.     *    and not followed links. Normaly a URL comparison has to
  116.     *    be done in this case.
  117.     */
  118.  
  119.    return(followed = !followed);
  120. }
  121. ///
  122. /// AppMsgFunc
  123. __saveds __asm LONG AppMsgFunc(register __a2 APTR obj, register __a1 struct AppMessage **x)
  124. {
  125.    /*
  126.     *   Very simple functions to parse the
  127.     *   AppMessage and set URL
  128.     *   (Taken from AppWindow.c)
  129.     */
  130.  
  131.    struct WBArg *ap;
  132.    struct AppMessage *amsg = *x;
  133.    static char buf[256];
  134.  
  135.    if(amsg->am_NumArgs)
  136.    {
  137.       ap=amsg->am_ArgList;
  138.       NameFromLock(ap->wa_Lock,buf,sizeof(buf));
  139.       AddPart(buf,ap->wa_Name,sizeof(buf));
  140.       set(obj,MUIA_HTMLtext_URL,buf);
  141.    }
  142.  
  143.    return(0);
  144. }
  145. ///
  146. /// Main
  147. int main(int argc,char *argv[])
  148. {
  149.    struct   Library *IntuitionBase;
  150.    int      ret=RETURN_ERROR;
  151.    static   const struct Hook AppMsgHook   = { { NULL,NULL },(VOID *)AppMsgFunc,NULL,NULL };
  152.    static   const struct Hook OpenURLHook  = { { NULL,NULL },(VOID *)OpenURL ,NULL,NULL };
  153.    static   const struct Hook CloseURLHook = { { NULL,NULL },(VOID *)CloseURL ,NULL,NULL };
  154.    static   const struct Hook VLinkHook    = { { NULL,NULL },(VOID *)VLink ,NULL,NULL };
  155.  
  156.    if(IntuitionBase = OpenLibrary("intuition.library", 36))
  157.    {
  158.       struct Library *MUIMasterBase;
  159.  
  160.       if(MUIMasterBase = OpenLibrary(MUIMASTER_NAME, 13))
  161.       {
  162.          ULONG signals;
  163.          BOOL running = TRUE;
  164.  
  165.          // Load the text
  166.          app = ApplicationObject,
  167.             MUIA_Application_Title      , "HTMLtext-DEMO",
  168.             MUIA_Application_Version    , "$VER: HTMLtext-DEMO 1.1 " __AMIGADATE__,
  169.             MUIA_Application_Copyright  , "(C)opyright by Dirk Holtwick 1997",
  170.             MUIA_Application_Author     , "Dirk Holtwick",
  171.             MUIA_Application_Description, "Uses the HTMLtext.mcc to display text.",
  172.             MUIA_Application_Base       , "HTMLTEXTDEMO",
  173.  
  174.             SubWindow, window = WindowObject,
  175.                MUIA_Window_Title, "HTMLtext-Demo © Dirk Holtwick, 1997",
  176.                MUIA_Window_ID   , MAKE_ID('D','E','M','O'),
  177.                MUIA_Window_UseRightBorderScroller, TRUE,
  178.                MUIA_Window_UseBottomBorderScroller, TRUE,
  179.                MUIA_Window_AppWindow, TRUE,
  180.  
  181.                WindowContents, VGroup,
  182.  
  183.                   Child, url = TextObject,
  184.                      TextFrame,
  185.                      MUIA_Font,        MUIV_Font_Tiny,
  186.                      MUIA_Background,  MUII_TextBack,
  187.                      End,
  188.  
  189.                   /*
  190.                    *   Try to put the HTMLtextObject in a ScrollgroupObject
  191.                    *   to allow the user an easy handling of the VirtualGroup
  192.                    *   created by HTMLtext.mcc.
  193.                    */
  194.  
  195.                   Child, scroll = ScrollgroupObject,
  196.                      MUIA_CycleChain, 1,
  197.                      MUIA_Scrollgroup_UseWinBorder, TRUE,
  198.                      MUIA_Scrollgroup_Contents, html = HTMLtextObject,
  199.                         TextFrame,
  200.                         MUIA_HTMLtext_Contents,       DEMOTEXT,
  201.                         MUIA_HTMLtext_OpenURLHook,    &OpenURLHook,
  202.                         MUIA_HTMLtext_CloseURLHook,   &CloseURLHook,
  203.                         MUIA_HTMLtext_VLinkHook,      &VLinkHook,
  204.                         End,
  205.                      End,
  206.  
  207.                   Child, str = StringObject,
  208.                      StringFrame,
  209.                      MUIA_ObjectID,          1,
  210.                      MUIA_CycleChain,        1,
  211.                      MUIA_String_MaxLen,     1024,
  212.                      MUIA_String_Contents,   DEMOTEXT,
  213.                      End,
  214.                   End,
  215.                End,
  216.             End;
  217.  
  218.          if(app)
  219.          {
  220.             // Window closing
  221.             DoMethod(window, MUIM_Notify, MUIA_Window_CloseRequest, TRUE,
  222.                app, 2, MUIM_Application_ReturnID, MUIV_Application_ReturnID_Quit);
  223.  
  224.             // after modification of string contents refresh HTML object
  225.             DoMethod(str,MUIM_Notify,MUIA_String_Contents,MUIV_EveryTime,
  226.                html,3,MUIM_Set,MUIA_HTMLtext_Contents,MUIV_TriggerValue);
  227.  
  228.             // string gadgets will always be active
  229.             DoMethod(str,MUIM_Notify,MUIA_String_Acknowledge,MUIV_EveryTime,
  230.                window,3,MUIM_Set,MUIA_Window_ActiveObject,str);
  231.  
  232.             // react if someone droped an icon on the HTML object
  233.             DoMethod(html,MUIM_Notify,MUIA_AppMessage,MUIV_EveryTime,
  234.                html,3,MUIM_CallHook,&AppMsgHook,MUIV_TriggerValue);
  235.  
  236.             // display title in windows title bar
  237.             DoMethod(html,MUIM_Notify,MUIA_HTMLtext_Title,MUIV_EveryTime,
  238.                window,3,MUIM_Set,MUIA_Window_Title,MUIV_TriggerValue);
  239.  
  240.             // display current URL
  241.             DoMethod(html,MUIM_Notify,MUIA_HTMLtext_URL,MUIV_EveryTime,
  242.                url,3,MUIM_Set,MUIA_Text_Contents,MUIV_TriggerValue);
  243.  
  244.             // ready to open the window ...
  245.             set(window,MUIA_Window_ActiveObject,   str);
  246.             set(window,MUIA_Window_DefaultObject,  html);
  247. //            set(str, MUIA_String_Contents, DEMOTEXT);
  248.  
  249.             // Load string
  250.             DoMethod(app, MUIM_Application_Load, MUIV_Application_Load_ENVARC);
  251.  
  252.             set(window,MUIA_Window_Open,TRUE);
  253.  
  254.             while (running)
  255.             {
  256.                switch(DoMethod(app,MUIM_Application_Input,&signals))
  257.                {
  258.                   case MUIV_Application_ReturnID_Quit:
  259.                      running = FALSE;
  260.                      break;
  261.                }
  262.  
  263.                if(running && signals)
  264.                   Wait(signals);
  265.             }
  266.  
  267.             set(window, MUIA_Window_Open, FALSE);
  268.  
  269.             DoMethod(app, MUIM_Application_Save, MUIV_Application_Save_ENVARC);
  270.  
  271.             MUI_DisposeObject(app);
  272.  
  273.             ret = RETURN_OK;
  274.          }
  275.          else
  276.          {
  277.             puts("Could not open application!");
  278.             ret = RETURN_FAIL;
  279.          }
  280.  
  281.          CloseLibrary(MUIMasterBase);
  282.       }
  283.       else
  284.       {
  285.          puts("Could not open muimaster.library v13!");
  286.          ret = RETURN_FAIL;
  287.       }
  288.  
  289.       CloseLibrary(IntuitionBase);
  290.    }
  291.    else
  292.    {
  293.       puts("Could not open intuition.library v36!");
  294.       ret = RETURN_FAIL;
  295.    }
  296.  
  297.    return(ret);
  298. }
  299. ///
  300.  
  301.